//
//  StoreDetailRequest.m
//  BaseProject
//
//  Created by 王洋 on 2020/12/29.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "StoreDetailRequest.h"

@implementation StoreDetailRequest

+ (void)request_storeDetailWith:(NSString *)sellerId comption:(void (^)(BOOL success, StoreDetailModel *model))comption {
    
    StoreDetailModel *model = [[StoreDetailModel alloc] init];
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:sellerId forKey:@"sellerId"];
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_StoreInfo];
    
    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在加载"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSArray *sysCouponList = [resultDic valueForKey:@"sysCouponList"];
                NSArray *sellerGoodsList = [resultDic valueForKey:@"sellerGoodsList"];
                NSArray *sellingSellerGoodsList = [resultDic valueForKey:@"sellingSellerGoodsList"];
                NSArray *sellerOpusList = [resultDic valueForKey:@"sellerOpusList"];
                NSArray *userEvaluateList = [resultDic valueForKey:@"userEvaluateList"];
                NSArray *sellerOpusClassificationList = [resultDic valueForKey:@"sellerOpusClassificationList"];
                NSArray *SellerPromiseLabelAuthernticationList = [resultDic valueForKey:@"SellerPromiseLabelAuthernticationList"];
                NSArray *sysWorryFreeLabel = [resultDic valueForKey:@"sysWorryFreeLabel"];
                NSDictionary *seller = [resultDic valueForKey:@"seller"];
                NSDictionary *sellerDynamic = [resultDic valueForKey:@"sellerDynamic"];
                NSDictionary *userCollection = [resultDic valueForKey:@"userCollection"];
                
                
                model.dynamicModel = [CommonDynamicModel modelWithDictionary:sellerDynamic];
                model.userCollection = [CommonFavorModel modelWithDictionary:userCollection];
                if (model.userCollection.isDel == 2) {
                    model.isCollect = YES;
                } else {
                    model.isCollect = NO;
                }
                
                if ([sellerDynamic isKindOfClass:[NSDictionary class]]) {
                    NSArray *imageUrls = [sellerDynamic valueForKey:@"imageUrls"];
                    if ([imageUrls isKindOfClass:[NSArray class]]) {
                        model.dynamicModel.imageUrls = @[].mutableCopy;
                        for (int i = 0; i < imageUrls.count; i++) {
                            NSDictionary *imgDic = imageUrls[i];
                            CommonImageModel *imgModel = [CommonImageModel modelWithDictionary:imgDic];
                            
                            if ([imgModel.type isEqualToString:@"dtimg"]) {
                                [model.dynamicModel.imageUrls addObject:imgModel];
                            }
                        }
                    }
                }

                model.seller = [CommonSellerModule modelWithDictionary:seller];
                
                if (model.seller.lat > 0 && model.seller.lng > 0) {
                    model.seller.distance = [Utilities calculateStart:CLLocationCoordinate2DMake([TTUserModel sharedObject].latitude, [TTUserModel sharedObject].longitude) end:CLLocationCoordinate2DMake(model.seller.lat,model.seller.lng)];
                }
                
                NSArray *coverPaths = [seller valueForKey:@"coverPaths"];
                model.seller.coverPaths = @[].mutableCopy;
                if ([coverPaths isKindOfClass:[NSArray class]]) {
                    for (int i = 0; i < coverPaths.count; i++) {
                        CommonImageModel *imgModel = [CommonImageModel modelWithDictionary:coverPaths[i]];
                        [model.seller.coverPaths addObject:imgModel];
                    }
                }
                
                model.sysCouponList = @[].mutableCopy;
                if ([sysCouponList isKindOfClass:[NSArray class]]) {
                    for (int i=0; i < sysCouponList.count; i++) {
                        NSDictionary *sysCouponListDic = sysCouponList[i];
                        CommonCouponModel *couponModel = [CommonCouponModel modelWithDictionary:sysCouponListDic];
                        [model.sysCouponList addObject:couponModel];
                    }
                }
//
                model.userEvaluateList = @[].mutableCopy;
                if ([userEvaluateList isKindOfClass:[NSArray class]]) {
                    for (int i=0; i < userEvaluateList.count; i++) {
                        NSDictionary *userEvaluateListDic = userEvaluateList[i];
                        NSDictionary *sellerGoods = [userEvaluateListDic valueForKey:@"sellerGoods"];
                        StoreDetailEvaluateModel *evaluateModel = [StoreDetailEvaluateModel modelWithDictionary:userEvaluateListDic];
                        NSArray *imageUrls = [userEvaluateListDic valueForKey:@"imageUrls"];
                        if ([imageUrls isKindOfClass:[NSArray class]]) {
                            evaluateModel.imageUrls = @[].mutableCopy;
                            for (int j = 0; j < imageUrls.count; j++) {
                                NSDictionary *imgDic = imageUrls[j];
                                CommonImageModel *imgModel = [CommonImageModel modelWithDictionary:imgDic];
                                NSString *url = [TT_OSSManager reSourceUrl:imgModel.url type:BA_resizeType_pro];
                                imgModel.url = url;
                                
                                [evaluateModel.imageUrls addObject:imgModel];
                            }
                        }
                        evaluateModel.sellerGoods = [CommonPackageModel modelWithJSON:sellerGoods];

                        evaluateModel.infoHeight = 60.0f;
                        evaluateModel.evaluateTextHeight = [Utilities getStringHeightWithText:evaluateModel.ueEvaluateContent font:[UIFont systemFontOfSize:12] viewWidth:TTScreenWidth-30];
                        NSInteger lineCount = evaluateModel.imageUrls.count/3;
                        if (evaluateModel.imageUrls.count%3>0) {
                            lineCount++;
                        }
                        evaluateModel.collectionViewHeight = lineCount * ((TTScreenWidth - 30)/3);
                        evaluateModel.thumbHeight = 40.0f;
                        
                        if (evaluateModel.sellerGoods.goodsId.length > 0) {
                            evaluateModel.packageHeight = 80.0f;
                        } else {
                            evaluateModel.packageHeight = CGFLOAT_MIN;
                        }
                        
                        evaluateModel.itemHeight = evaluateModel.infoHeight + evaluateModel.evaluateTextHeight + evaluateModel.collectionViewHeight + evaluateModel.packageHeight + evaluateModel.thumbHeight;
                        
                        [model.userEvaluateList addObject:evaluateModel];
                    }
                }
                
                model.sellerGoodsList = @[].mutableCopy;
                if ([sellerGoodsList isKindOfClass:[NSArray class]]) {
                    
                    for (int i = 0; i < sellerGoodsList.count; i++) {
                        NSDictionary *sellerGoodsListDic = sellerGoodsList[i];
                        CommonPackageModel *packageModel = [CommonPackageModel modelWithJSON:sellerGoodsListDic];
                        NSString *url = [TT_OSSManager reSourceUrl:packageModel.url type:BA_resizeType_pro];
                        packageModel.url = url;
                        
                        [model.sellerGoodsList addObject:packageModel];
                    }
                }
                
                model.sellingSellerGoodsList = @[].mutableCopy;
                if ([sellingSellerGoodsList isKindOfClass:[NSArray class]]) {
                    for (int i = 0; i < sellingSellerGoodsList.count; i++) {
                        NSDictionary *sellingSellerGoodsListDic = sellingSellerGoodsList[i];
                        CommonPackageModel *packageModel = [CommonPackageModel modelWithJSON:sellingSellerGoodsListDic];
                        NSString *url = [TT_OSSManager reSourceUrl:packageModel.url type:BA_resizeType_plus];
                        packageModel.url = url;
                        
                        [model.sellingSellerGoodsList addObject:packageModel];
                    }
                }
                
                model.sellerOpusList = @[].mutableCopy;
                if ([sellerOpusList isKindOfClass:[NSArray class]]) {
                    for (int i = 0; i < sellerOpusList.count; i++) {
                        NSDictionary *sellerOpusListDic = sellerOpusList[i];
                        CommonOpusModel *opusModel = [CommonOpusModel modelWithDictionary:sellerOpusListDic];
                        NSString *url = [TT_OSSManager reSourceUrl:opusModel.transverseImg type:BA_resizeType_max];
                        opusModel.transverseImg = url;
                        
                        [model.sellerOpusList addObject:opusModel];
                    }
                }
                
                model.sellerOpusClassificationList = @[].mutableCopy;
                if ([sellerOpusClassificationList isKindOfClass:[NSArray class]]) {
                    for (int i = 0; i < sellerOpusClassificationList.count; i++) {
                        NSDictionary *sellerOpusClassificationListDic = sellerOpusClassificationList[i];
                        CommonOpusCatModel *opusCatModel = [CommonOpusCatModel modelWithDictionary:sellerOpusClassificationListDic];
                        if (i==0) {opusCatModel.isSelect = YES;}
                        else {opusCatModel.isSelect = NO;}
                        opusCatModel.itemWidth = [Utilities getStringWidthWithString:opusCatModel.opusClassName fontSize:12 andHeight:30] + 20;
                        [model.sellerOpusClassificationList addObject:opusCatModel];
                    }
                }
                
                model.SellerPromiseLabelAuthernticationList = @[].mutableCopy;
                if ([SellerPromiseLabelAuthernticationList isKindOfClass:[NSArray class]]) {
                    for (int i = 0; i < SellerPromiseLabelAuthernticationList.count; i++) {
                        NSDictionary *SellerPromiseLabelAuthernticationListDic = SellerPromiseLabelAuthernticationList[i];
                        CommonPromiseTagModel *tagModel = [CommonPromiseTagModel modelWithDictionary:SellerPromiseLabelAuthernticationListDic];
                        [model.SellerPromiseLabelAuthernticationList addObject:tagModel];
                    }
                }
                
                model.sysWorryFreeLabel = @[].mutableCopy;
                if ([sysWorryFreeLabel isKindOfClass:[NSArray class]]) {
                    for (int i = 0; i < sysWorryFreeLabel.count; i++) {
                        NSDictionary *sysWorryFreeLabelDic = sysWorryFreeLabel[i];
                        CommonWYXTagModel *tagModel = [CommonWYXTagModel modelWithDictionary:sysWorryFreeLabelDic];
                        [model.sysWorryFreeLabel addObject:tagModel];
                    }
                }
                
                comption(YES,model);
            }
        } else {
            
            comption(NO,nil);
            [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
            [SVProgressHUD dismissWithDelay:1];
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
        [SVProgressHUD dismiss];
        comption(NO,nil);

    }];
    
    
    
    
}

@end
